package com.example.administrator.szb.util;

import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.android.tu.loadingdialog.LoadingDailog;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.callbackconfig.XFCCallBack;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.zhouwei.library.CustomPopWindow;

import java.util.Timer;
import java.util.TimerTask;


/**
 * Created by D N on 2017/9/9.
 */

public class DialogUtil {
    private static AlertDialog.Builder alertDialog;
    public static LoadingDailog dialog_ios;
    public static CustomPopWindow loading;//进度条
    public static AlertDialog.Builder builder;
    public static LoadingDailog.Builder loadBuilder;
    public static AlertDialog dialog_all;
    public static LoadingDailog dialog;
    public static WindowManager mWindowManager;

    public static CustomPopWindow popWindow;//进度条

    private static final int REQUEST_PERMISSION_CODE = 1;

    public static void  showSimpleDialog(Context context,String message){
        alertDialog = new AlertDialog.Builder(context);
        alertDialog.setMessage(message).show();
    }
    /**
     * 显示进度条
     */
    public static void showProgressbar(Context context,int rootview_layout) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_popuwindow_loading, null);
        View rootview = LayoutInflater.from(context).inflate(rootview_layout, null);
        loading = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(view)//显示的布局，还可以通过设置一个View
                .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                .setBgDarkAlpha(0.7f) // 控制亮度
                .size(500, 150) //设置显示的大小，不设置就默认包裹内容
                .setFocusable(true)//是否获取焦点，默认为ture
                .setOutsideTouchable(true)//是否PopupWindow 以外触摸dissmiss
                .create()//创建PopupWindow
                .showAtLocation(rootview, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    /**
     * 仿iso加载效果
     */
    public static void showIsoProgressbar(Context context,String info){
        loadBuilder=new LoadingDailog.Builder(context)
                .setMessage(""+info)
                .setCancelable(true)
                .setCancelOutside(true);
        dialog_ios=loadBuilder.create();
        dialog_ios.show();
    }


    /**
     * 全局弹框
     */
    public static void showAllPopuwindow(int layout_id, XFCCallBack xfcCallBack){
        Context context = SampleApplicationLike.getInstance();

        View view = View.inflate(context, layout_id, null);

        xfcCallBack.initView(view,context);
        // 获取WindowManager
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);


        final WindowManager.LayoutParams params = new WindowManager.LayoutParams();
        // 类型
        params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        // 设置flag
        params.flags = WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
        // 如果设置了WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE，弹出的View收不到Back键的事件
        // 不设置这个弹出框的透明遮罩显示为黑色
        params.format = PixelFormat.TRANSLUCENT;
        // FLAG_NOT_TOUCH_MODAL不阻塞事件传递到后面的窗口
        // 设置 FLAG_NOT_FOCUSABLE 悬浮窗口较小时，后面的应用图标由不可长按变为可长按
        // 不设置这个flag的话，home页的划屏会有问题
        params.width =  ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.MATCH_PARENT;
        params.gravity = Gravity.CENTER;

        mWindowManager.addView(view, params);
    }


    public static void showToast(Context context, String message) {
        //加载Toast布局
        View toastRoot = LayoutInflater.from(context).inflate(R.layout.toast, null);
        //初始化布局控件

        ((TextView) toastRoot.findViewById(R.id.toast_message)).setText(message);
        //Toast的初始化
        final Toast toastStart = new Toast(context);
        //获取屏幕高度
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        //Toast的Y坐标是屏幕高度的1/3，不会出现不适配的问题
        toastStart.setGravity(Gravity.TOP, 0, height / 3);
        toastStart.setDuration(Toast.LENGTH_SHORT);
        toastStart.setView(toastRoot);
        //toastStart.show();

        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                toastStart.show();
            }
        }, 0, 2500);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                toastStart.cancel();
                timer.cancel();
            }
        },2500);
    }


    /**
     * 初始化popupwindow弹出框
     */
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    public static void showPopupwindow(Context context,int view_layout_id, int width, int height, int rootview_layout_id, BaseActivity.CallbackPopuwindow callback) {
        View popwindow_view = LayoutInflater.from(context).inflate(view_layout_id, null);
        View rootview = LayoutInflater.from(context).inflate(rootview_layout_id, null);
        //处理popWindow 显示内容
        callback.doSomeThing(popwindow_view);
        if (view_layout_id != R.layout.view_progressbar) {
            popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                    .setView(popwindow_view)//显示的布局，还可以通过设置一个View
                    .enableBackgroundDark(true) //弹出popWindow时，背景是否变暗
                    .setBgDarkAlpha(0.4f) // 控制亮度
                    .size(width, height) //设置显示的大小，不设置就默认包裹内容
                    .setFocusable(true)//是否获取焦点，默认为ture
                    .setOutsideTouchable(true)//是否PopupWindow 以外触摸dissmiss
                    .create()//创建PopupWindow
                    .showAtLocation(rootview, Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL, 0, 0);
        }
        //显示PopupWindow

    }
}
